home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 April / april_2001.iso / intercd / root / ^Palm / Games / eCross / src / Info.java < prev    next >
Encoding:
Java Source  |  2000-07-28  |  5.6 KB  |  241 lines

  1. /*
  2.  * Info.java - Provides an info button
  3.  * eCross is Copyright (C) 2000 Romain Guy
  4.  * guy.romain@bigfoot.com
  5.  * www.jext.org
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version 2
  10.  * of the License, or any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  */
  21.  
  22. import waba.fx.*;
  23. import waba.io.*;
  24.  
  25. /**
  26.  * A control that displays an info button
  27.  * @version 1.0
  28.  * @author Romain Guy <guy.romain@bigfoot.com>
  29.  */
  30.  
  31. public class Info
  32. {
  33.   private byte state = 0;
  34.   private byte page, maxPages;
  35.   private boolean lGray, rGray;
  36.  
  37.   private Catalog infos;
  38.  
  39.   // info
  40.   private boolean inverted = false;
  41.   private Image iImage = new Image("datas/info.bmp");
  42.   private Image niImage = new Image("datas/ninfo.bmp");
  43.  
  44.   // arrows
  45.   private Image leftArrow = new Image("datas/larrow.bmp");
  46.   private Image leftGrayedArrow = new Image("datas/glarrow.bmp");
  47.   private Image rightArrow = new Image("datas/rarrow.bmp");
  48.   private Image rightGrayedArrow = new Image("datas/grarrow.bmp");
  49.  
  50.   /**
  51.    * Creates a new info button.
  52.    */
  53.  
  54.   public Info()
  55.   {
  56.   }
  57.  
  58.   /**
  59.    * Handles a pen down event.
  60.    * @param parent The caller
  61.    * @param g The <code>Graphics</code> surface of the caller
  62.    * @param x The x coordinate of the event
  63.    * @param y The y coordinate of the event
  64.    * @return <code>true</code> if the event was handled
  65.    */
  66.  
  67.   public boolean handlePenEvent(eCross parent, Graphics g, int x, int y)
  68.   {
  69.     // pages flipping
  70.     if (state == 1)
  71.     {
  72.       if (x > 13 && x < 43 && y > 121 && y < 136)
  73.       {
  74.  
  75.         state = 0;
  76.         infos.close();
  77.         parent.draw();
  78.         parent.resumeTimer();
  79.  
  80.       } else if (!lGray && x > 128 && x < 134 && y > 125 && y < 136) {
  81.  
  82.         if (--page == 0)
  83.         {
  84.           g.drawImage(leftGrayedArrow, 128, 125);
  85.           lGray = true;
  86.         }
  87.  
  88.         if (rGray)
  89.         {
  90.           g.drawImage(rightArrow, 140, 125);
  91.           rGray = false;
  92.         }
  93.  
  94.         paintPage(g);
  95.  
  96.       } else if (!rGray && x > 140 && x < 146 && y > 125 && y < 136) {
  97.  
  98.         if (++page == maxPages - 1)
  99.         {
  100.           g.drawImage(rightGrayedArrow, 140, 125);
  101.           rGray = true;
  102.         }
  103.  
  104.         if (lGray)
  105.         {
  106.           g.drawImage(leftArrow, 128, 125);
  107.           lGray = false;
  108.         }
  109.  
  110.         paintPage(g);
  111.       }
  112.  
  113.       return true;
  114.     }
  115.  
  116.     if (x > 160 - (10 + 1) && x < 160 - 1 && y > 2 && y < 12)
  117.     {
  118.       // the info icon was tapped
  119.       inverted = true;
  120.       g.drawImage(niImage, 160 - (10 + 1), 2);
  121.       return true;
  122.     }
  123.  
  124.     return false;
  125.   }
  126.  
  127.   public boolean handlePenUpEvent(Graphics g, int x, int y)
  128.   {
  129.     if (inverted)
  130.     {
  131.       inverted = false;
  132.       g.drawImage(iImage, 160 - (10 + 1), 2);
  133.  
  134.       return true; // (x > 160 - (10 + 1) && x < 160 - 1 && y > 2 && y < 12);
  135.     }
  136.  
  137.     return false;
  138.   }
  139.  
  140.   /**
  141.    * Paints infos.
  142.    */
  143.  
  144.   public void showInfo(eCross parent, Graphics g)
  145.   {
  146.     parent.pauseTimer();
  147.  
  148.     state = 1;
  149.     page = 0;
  150.     lGray = true;
  151.     rGray = false;
  152.  
  153.     g.setColor(255, 255, 255);
  154.     // erase place of the window
  155.     g.fillRect(9, 19, 142, 122);
  156.  
  157.     g.setColor(0, 0, 0);
  158.     g.drawRect(10, 20, 140, 120);
  159.  
  160.     Font font = new Font("Helvetica", Font.BOLD, 10);
  161.     FontMetrics fm = parent.getFontMetrics(font);
  162.     g.setFont(font);
  163.  
  164.     g.drawRect(13, 121, 30, 15);
  165.     g.drawDots(15, 136, 43, 136);
  166.     g.drawDots(43, 135, 43, 123);
  167.     String text = new String("Ok");
  168.     g.drawText(text, 13 + (30 - fm.getTextWidth(text)) / 2, 123);
  169.  
  170.     g.drawImage(leftGrayedArrow, 128, 125);
  171.     g.drawImage(rightArrow, 140, 125);
  172.  
  173.     infos = new Catalog("eCross Infos.eCrs.eCrs", Catalog.READ_ONLY);
  174.     if (!infos.isOpen())
  175.       return;
  176.     infos.setRecordPos(0);
  177.     byte[] b = new byte[1];
  178.     infos.readBytes(b, 0, 1);
  179.     maxPages = b[0];
  180.  
  181.     g.setFont(new Font("Helvetica", Font.PLAIN, 10));
  182.     paintPage(g);
  183.   }
  184.  
  185.   // paints current informations page
  186.  
  187.   private void paintPage(Graphics g)
  188.   {
  189.     if (!infos.setRecordPos(page + 1))
  190.       return;
  191.  
  192.     g.setColor(255, 255, 255);
  193.     g.fillRect(11, 21, 138, 100);
  194.  
  195.     g.setColor(0, 0, 0);
  196.  
  197.     int last = -1, count = 0;
  198.  
  199.     byte[] b = new byte[infos.getRecordSize()];
  200.     infos.readBytes(b, 0, b.length);
  201.  
  202.     char[] _page = new char[b.length];
  203.     for (int i = 0; i < b.length; i++)
  204.       _page[i] = (char) b[i];
  205.     b = null;
  206.  
  207.     int len = _page.length;
  208.  
  209.     for (int i = 0; i < len; i++)
  210.     {
  211.       if (_page[i] == '\n')
  212.       {
  213.         g.drawText(new String(_page, ++last, i - last), 13, 21 + count * 10 + (count + 1));
  214.         count++;
  215.         last = i;
  216.       }
  217.     }
  218.  
  219.     _page = null;
  220.     g.drawText((page + 1) + "/" + maxPages, 75, 123);
  221.   }
  222.  
  223.   public void paint(Graphics g)
  224.   {
  225.     g.drawImage(iImage, 160 - (10 + 1), 2);
  226.   }
  227.  
  228.   public void free()
  229.   {
  230.     iImage.free();
  231.     niImage.free();
  232.  
  233.     leftArrow.free();
  234.     leftGrayedArrow.free();
  235.     rightArrow.free();
  236.     rightGrayedArrow.free();
  237.   }
  238. }
  239.  
  240. // End of Info.java
  241.